<?php
// 首页显示信息
namespace Store\Controller;
use Think\Controller;
class IndexController extends CommonController {
    function index(){
       $this->assign('title','个人信息');
       $this->display();
    }
    function get_top_info(){
    	$store_id=session('store_id');
    	$order_goods=M('order_goods');
    	$order=M('order');
    	$shop=M('shop');
    	
    	//粉丝数
    	$userArray=$order_goods->where("store_id=$store_id")->group('buyer_id')->select();
    	$count_user=count($userArray);
    	//今日订单总金额(元)
    	$today=today();
        $start_date = $today[0];
        $end_date = $today[1];
        $map['add_time']  = array('between',array($start_date,$end_date));
        $map['order_state']  = array('egt','20');
        $map['store_id']  = array('eq',$store_id);
        $today_count = $order->where($map)->sum('order_amount');
        //商品发布(个)
        $count_shop=$shop->where("store_id=$store_id")->count();
        //订单总数(笔)
        $count_order=$order->where("store_id=$store_id")->count();
        //本月订单笔数
        $month=month();
        $start=$month[0];
        $end=$month[1];
        $map2['add_time']  = array('between',array($start,$end));
        $map2['store_id']  = array('eq',$store_id);
        $month_order = $order->where($map2)->count();
        //已完成交易(笔)
    	$ycpay_order=$order->where("order_state='40' and refund_state=0 and store_id=$store_id")->count();
    	$count_user=$count_user?$count_user:0;
    	$today_count=$today_count?$today_count:0.00;
    	$count_shop=$count_shop?$count_shop:0;
    	$count_order=$count_user?$count_user:0;
    	$month_order=$month_order?$month_order:0;
    	$ycpay_order=$ycpay_order?$ycpay_order:0;
    	
    	$result = array(
            "count_user"=>$count_user,
            "today_count"=>$today_count,
            "count_shop"=>$count_shop,
            "count_order"=>$count_order,
            "month_order"=>$month_order,
            "ycpay_order"=>$ycpay_order,
        );
        $this->ajaxReturn($result);
    	
    }
    
    function get_order(){
    	$store_id=session('store_id');
    	$order=M('order');
    	//未付款
		$wpay_order=$order->where("order_state='10' and refund_state=0 and store_id=$store_id")->count();
		//已付款
		$ypay_order=$order->where("order_state='20' and refund_state=0 and store_id=$store_id")->count();
		//已发货
		$yfpay_order=$order->where("order_state='30' and refund_state=0 and store_id=$store_id")->count();
		//已签收
		$ycpay_order=$order->where("order_state='40' and refund_state=0 and store_id=$store_id")->count();
		//已评价
		$yepay_order=$order->where("order_state='40' and evaluation_state>0 and store_id=$store_id")->count();
		$wepay_order=$order->where("order_state='40' and evaluation_state=0 and store_id=$store_id")->count();
		//已关闭
		$close_order=$order->where("order_state='0' and store_id=$store_id")->count();
		//退款中
		$back_order=$order->where("refund_state>0 and refund_state<5 and store_id=$store_id")->count();
		//退款完成
		$back_order2=$order->where("refund_state=5 and store_id=$store_id")->count();
		
		$wpay_order=$wpay_order?$wpay_order:0;
    	$ypay_order=$ypay_order?$ypay_order:0.00;
    	$yfpay_order=$yfpay_order?$yfpay_order:0;
    	$ycpay_order=$ycpay_order?$ycpay_order:0;
    	$yepay_order=$yepay_order?$yepay_order:0;
    	$close_order=$close_order?$close_order:0;
    	$back_order=$back_order?$back_order:0;
    	$back_order2=$back_order2?$back_order2:0;
    
    	
		$result = array(
            "wpay_order"=>$wpay_order,
            "ypay_order"=>$ypay_order,
            "yfpay_order"=>$yfpay_order,
            "ycpay_order"=>$ycpay_order,
            "yepay_order"=>$yepay_order,
            "close_order"=>$close_order,
            "back_order"=>$back_order,
            "back_order2"=>$back_order2,
        );
        $this->ajaxReturn($result);
    }
    
    function get_yestoday_order(){
    	$store_id=session('store_id');
    	$order=M('order');
    	
    	$month=month();
        $start=$month[0];
        $end=$month[1];
        
        
		$yesterday=yesterday();
        $start_date =$yesterday[0];
        $end_date =$yesterday[1];
        
        $map['add_time']  = array('between',array($start_date,$end_date));
		$map['order_state']  = array('egt','20');
		$count_price = $order->where($map)->sum('order_amount');
		$count_num = $order->where($map)->count();
		
		
		$map2['add_time']  = array('between',array($start,$end));
		$map2['order_state']  = array('egt','20');
		$m_count_price = $order->where($map2)->sum('order_amount');
		$m_count_num = $order->where($map2)->count();
		
    	$count_price=$count_price?$count_price:0;
    	$count_num=$count_num?$count_num:0;
    	$m_count_price=$m_count_price?$m_count_price:0;
    	$m_count_num=$m_count_num?$m_count_num:0;
		$result = array(
            "count_price"=>$count_price,
            "count_num"=>$count_num,
            "m_count_price"=>$m_count_price,
            "m_count_num"=>$m_count_num
        );
        $this->ajaxReturn($result);
    }
    
    function getorderchartcount(){
    	
        $type = isset($_POST['date']) ? $_POST['date'] : 4;
        $order=M('order');
        $data = array();
        if ($type == 1) {
            $today=today();
            for ($i = 0; $i < 24; $i ++) {
            	$start=$today[0];
                $date_start = $start + 3600 * $i;
                $date_end = $start + 3600 * ($i + 1);
                $map['add_time']  = array('between',array($date_start,$date_end));
				$map['order_state']  = array('egt','20');
                $count = $order->where($map)->count();
                $data[$i] = array(
                    $i . ':00',
                   (int)$count
                );
            }
        } elseif ($type == 2) {
            $yesterday=yesterday();
            $start=$yesterday[0];
            for ($j = 0; $j < 24; $j ++) {
                $date_start = $start + 3600 * $j;
                $date_end =$start + 3600 * ($j + 1);
                $map['add_time']  = array('between',array($date_start,$date_end));
				$map['order_state']  = array('egt','20');
                $count = $order->where($map)->count();
                $data[$j] = array(
                    $j . ':00',
                   (int)$count
                );
            }
        } elseif ($type == 3) {
            $week=week();
            $start=$week[0];
            $start = $start - 604800;
            for ($j = 0; $j < 7; $j ++) {
                $date_start = $start + 86400 * $j;
                $date_end = $start + 86400 * ($j + 1);
               $map['add_time']  = array('between',array($date_start,$date_end));
				$map['order_state']  = array('egt','20');
                $count = $order->where($map)->count();
                $data[$j] = array(
                    '星期' . ($j + 1),
                    (int)$count
                );
            }
        } elseif ($type == 4) {
            $month=month();
            $start=$month[0];
            $end=$month[1];
            for ($j = 0; $j < ($end + 1 - $start) / 86400; $j ++) {
                $date_start =$start + 86400 * $j;
                $date_end = $start + 86400 * ($j + 1);
                $map['add_time']  = array('between',array($date_start,$date_end));
				$map['order_state']  = array('egt','20');
                $count = $order->where($map)->count();
                $data[$j] = array(
                    (1 + $j) . '日',
                    (int)$count
                );
            }
        }
       $this->ajaxReturn($data);
    }
   
	
    
    
}